package com.tsf.demo.tools.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tencent.cloud.task.sdk.core.utils.DateUtil;
import com.tsf.demo.tools.service.DiscoveryClientSerivce;
import com.tsf.demo.tools.service.Register;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//tangfuzhong
@RestController
public class RegisterController {

	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	private DiscoveryClientSerivce discoveryClientSerivce;

	private Logger logger = LoggerFactory.getLogger(this.getClass());


	@RequestMapping(value = "/echo-register", method = RequestMethod.GET)
	public String register(@RequestParam(value="flag",required=true) String flag,
						   @RequestParam(value="token",required=true) String token) {
		String url = "http://192.168.1.190:8000/v1/agent/service/register?token="+token;
		//url = "http://www.baidu.com";
		try {
			String content = getContent();
			JsonObject jsonObject = new JsonParser().parse(content).getAsJsonObject();
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);
			HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers);
			headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);
			if (flag.equals("1"))
			{
				ResponseEntity<String> response = new RestTemplate().exchange(
						url,
						HttpMethod.PUT, entity, String.class);
				Gson gson = new Gson();
				String json = gson.toJson(response);
				return json;
			}
			else
			{
				new RestTemplate().put(url,entity);

				return "success";
			}
		}
		catch (Exception e)
		{
			Gson gson = new Gson();
			String json = gson.toJson(e);
			return json;
		}

	}


	//提交注册的json内容
	public static int I_PORT = 20000;
	private static synchronized String getContent()
	{
		if (I_PORT>60000)
		{
			I_PORT = 20000;
		}
		I_PORT++;
		long createTime = System.currentTimeMillis();
		String content = "{\"ID\":\"test"+createTime+"\",\"Name\":\""+Register.testName+createTime+"\",\"Address\":\"192.168.1.204\",\"Port\":"+I_PORT+",\"Check\":{\"TTL\":\"60s\",\"Meta\":{\"TSF_API_METAS\":\"test\",\"url\":\"test\",\"TSF_APPLICATION_ID\":\"application-qby8ngyk\",\"TSF_GROUP_ID\":\"group-nqab2wal\",\"TSF_INSTNACE_ID\":\"192.168.1.204\",\"TSF_PROG_VERSION\":\"v1\",\"TSF_REGION\":\"tsfregion-mjy9mvgz\",\"TSF_ZONE\":\"\"}}}";
		return content;
	}


}